#include "24cxx.h" 
//#include "delay.h" 
#include "delay.h"
//Mini STM32¿ª·¢°å
//24CXXÇý¶¯º¯Êý(ÊÊºÏ24C01~24C16,24C32~256Î´¾­¹ý²âÊÔ!ÓÐ´ýÑéÖ¤!)
//ÕýµãÔ­×Ó@ALIENTEK
//2010/6/10
//V1.2

//³õÊ¼»¯IIC½Ó¿Ú
void AT24CXX_Init(void)
{
    IIC_Init();
}
//ÔÚAT24CXXÖ¸¶¨µØÖ·¶Á³öÒ»¸öÊý¾Ý
//ReadAddr:¿ªÊ¼¶ÁÊýµÄµØÖ·  
//·µ»ØÖµ  :¶Áµ½µÄÊý¾Ý
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
    u8 temp = 0;
    IIC_Start();
    if (EE_TYPE > AT24C16)
    {
        IIC_Send_Byte(0XA0);	   //·¢ËÍÐ´ÃüÁî
        IIC_Wait_Ack();
        IIC_Send_Byte(ReadAddr >> 8);//·¢ËÍ¸ßµØÖ·
        IIC_Wait_Ack();
    }
    else IIC_Send_Byte(0XA0 + ((ReadAddr / 256) << 1));   //·¢ËÍÆ÷¼þµØÖ·0XA0,Ð´Êý¾Ý 	 

    IIC_Wait_Ack();
    IIC_Send_Byte(ReadAddr % 256);   //·¢ËÍµÍµØÖ·
    IIC_Wait_Ack();
    IIC_Start();
    IIC_Send_Byte(0XA1);           //½øÈë½ÓÊÕÄ£Ê½			   
    IIC_Wait_Ack();
    temp = IIC_Read_Byte(0);
    IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ	    
    return temp;
}
//ÔÚAT24CXXÖ¸¶¨µØÖ·Ð´ÈëÒ»¸öÊý¾Ý
//WriteAddr  :Ð´ÈëÊý¾ÝµÄÄ¿µÄµØÖ·    
//DataToWrite:ÒªÐ´ÈëµÄÊý¾Ý
void AT24CXX_WriteOneByte(u16 WriteAddr, u8 DataToWrite)
{
    IIC_Start();
    if (EE_TYPE > AT24C16)
    {
        IIC_Send_Byte(0XA0);	    //·¢ËÍÐ´ÃüÁî
        IIC_Wait_Ack();
        IIC_Send_Byte(WriteAddr >> 8);//·¢ËÍ¸ßµØÖ·
    }
    else
    {
        IIC_Send_Byte(0XA0 + ((WriteAddr / 256) << 1));   //·¢ËÍÆ÷¼þµØÖ·0XA0,Ð´Êý¾Ý 
    }
    IIC_Wait_Ack();
    IIC_Send_Byte(WriteAddr % 256);   //·¢ËÍµÍµØÖ·
    IIC_Wait_Ack();
    IIC_Send_Byte(DataToWrite);     //·¢ËÍ×Ö½Ú							   
    IIC_Wait_Ack();
    IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ 
    delay_ms(10);
}
//ÔÚAT24CXXÀïÃæµÄÖ¸¶¨µØÖ·¿ªÊ¼Ð´Èë³¤¶ÈÎªLenµÄÊý¾Ý
//¸Ãº¯ÊýÓÃÓÚÐ´Èë16bit»òÕß32bitµÄÊý¾Ý.
//WriteAddr  :¿ªÊ¼Ð´ÈëµÄµØÖ·  
//DataToWrite:Êý¾ÝÊý×éÊ×µØÖ·
//Len        :ÒªÐ´ÈëÊý¾ÝµÄ³¤¶È2,4
void AT24CXX_WriteLenByte(u16 WriteAddr, u32 DataToWrite, u8 Len)
{
    u8 t;
    for (t = 0;t < Len;t++)
    {
        AT24CXX_WriteOneByte(WriteAddr + t, (DataToWrite >> (8 * t)) & 0xff);
    }
}

//ÔÚAT24CXXÀïÃæµÄÖ¸¶¨µØÖ·¿ªÊ¼¶Á³ö³¤¶ÈÎªLenµÄÊý¾Ý
//¸Ãº¯ÊýÓÃÓÚ¶Á³ö16bit»òÕß32bitµÄÊý¾Ý.
//ReadAddr   :¿ªÊ¼¶Á³öµÄµØÖ· 
//·µ»ØÖµ     :Êý¾Ý
//Len        :Òª¶Á³öÊý¾ÝµÄ³¤¶È2,4
u32 AT24CXX_ReadLenByte(u16 ReadAddr, u8 Len)
{
    u8 t;
    u32 temp = 0;
    for (t = 0;t < Len;t++)
    {
        temp <<= 8;
        temp += AT24CXX_ReadOneByte(ReadAddr + Len - t - 1);
    }
    return temp;
}
//¼ì²éAT24CXXÊÇ·ñÕý³£
//ÕâÀïÓÃÁË24XXµÄ×îºóÒ»¸öµØÖ·(255)À´´æ´¢±êÖ¾×Ö.
//Èç¹ûÓÃÆäËû24CÏµÁÐ,Õâ¸öµØÖ·ÒªÐÞ¸Ä
//·µ»Ø1:¼ì²âÊ§°Ü
//·µ»Ø0:¼ì²â³É¹¦
u8 AT24CXX_Check(void)
{
    u8 temp;
    temp = AT24CXX_ReadOneByte(255);//±ÜÃâÃ¿´Î¿ª»ú¶¼Ð´AT24CXX			   
    if (temp == 0X55)return 0;
    else//ÅÅ³ýµÚÒ»´Î³õÊ¼»¯µÄÇé¿ö
    {
        AT24CXX_WriteOneByte(255, 0X55);
        temp = AT24CXX_ReadOneByte(255);
        if (temp == 0X55)return 0;
    }
    return 1;
}

//ÔÚAT24CXXÀïÃæµÄÖ¸¶¨µØÖ·¿ªÊ¼¶Á³öÖ¸¶¨¸öÊýµÄÊý¾Ý
//ReadAddr :¿ªÊ¼¶Á³öµÄµØÖ· ¶Ô24c02Îª0~255
//pBuffer  :Êý¾ÝÊý×éÊ×µØÖ·
//NumToRead:Òª¶Á³öÊý¾ÝµÄ¸öÊý
void AT24CXX_Read(u16 ReadAddr, u8* pBuffer, u16 NumToRead)
{
    while (NumToRead)
    {
        *pBuffer++ = AT24CXX_ReadOneByte(ReadAddr++);
        NumToRead--;
    }
}
//ÔÚAT24CXXÀïÃæµÄÖ¸¶¨µØÖ·¿ªÊ¼Ð´ÈëÖ¸¶¨¸öÊýµÄÊý¾Ý
//WriteAddr :¿ªÊ¼Ð´ÈëµÄµØÖ· ¶Ô24c02Îª0~255
//pBuffer   :Êý¾ÝÊý×éÊ×µØÖ·
//NumToWrite:ÒªÐ´ÈëÊý¾ÝµÄ¸öÊý
void AT24CXX_Write(u16 WriteAddr, u8* pBuffer, u16 NumToWrite)
{
    while (NumToWrite--)
    {
        AT24CXX_WriteOneByte(WriteAddr, *pBuffer);
        WriteAddr++;
        pBuffer++;
    }
}












